package com.inossem.warehousestorage_inossem.adapter.inventory;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.inventory.InventoryExtra;
import com.inossem.warehousestorage_inossem.activity.inventory.InventoryItemAddActivity;
import com.inossem.warehousestorage_inossem.bean.inventory.InventorySpeInvCodeListBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inventory.InventoryManageRequest;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class InventorySelectSpecialInvCodeAdapter extends BaseAdapter {

    private Activity mContext;
    private List<InventorySpeInvCodeListBean.SpecialInventoryListBean> list = new ArrayList<InventorySpeInvCodeListBean.SpecialInventoryListBean>();
    private LayoutInflater inflater;
    private String code, specialInventoryFlag;
    private EditText editText;

    public InventorySelectSpecialInvCodeAdapter(Activity context, String specialInventoryFlag) {
        this.mContext = context;
        this.specialInventoryFlag = specialInventoryFlag;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_inventory_select_list_first_item, null);
            editText = (EditText) convertView.findViewById(R.id.code);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            editText.setHint(R.string.activity_inventory_select_speciallnv_code_please_input_special_inventory_code);
            if(!TextUtils.isEmpty(code)){
                editText.setText(code);
            }
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    InventorySelectSpecialInvCodeAdapter.this.code = editText.getText().toString();
                    getData(mContext, InventorySelectSpecialInvCodeAdapter.this.code);
                }
            });
        } else {
            InventorySpeInvCodeListBean.SpecialInventoryListBean materielItem = list.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_inventory_select_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, materielItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.code = (TextView) view.findViewById(R.id.code);
        viewHolder.description = (TextView) view.findViewById(R.id.description);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, InventorySpeInvCodeListBean.SpecialInventoryListBean item) {
        viewHolder.code.setText(item.getSpec_stock_code());
        viewHolder.description.setText(item.getSpec_stock_name());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent();
                intent.putExtra(InventoryExtra.CODE, item.getSpec_stock_code());
                intent.putExtra(InventoryExtra.DESCRIPTION, item.getSpec_stock_name());
                mContext.setResult(InventoryItemAddActivity.SELECT_POSITION, intent);
                mContext.finish();
            }
        });
    }

    private class ViewHolder {
        private TextView code, description;
    }

    public void search(String condition){
        code = condition;
        getData(mContext, condition);
    }

    private void getData(Activity activity, String condition) {
        InventoryManageRequest.findSpeInvCode(activity, condition, specialInventoryFlag, new GsonBeanListener<InventorySpeInvCodeListBean>() {
            @Override
            public void onSuccess(InventorySpeInvCodeListBean bean) {
                if(bean!=null){
                    InventorySelectSpecialInvCodeAdapter.this.list = bean.getBody();
                    InventorySelectSpecialInvCodeAdapter.this.notifyDataSetChanged();
                }else{
                    Utils.middleToast(activity,mContext.getResources().getString(R.string.activity_inventory_select_special_code_the_special_inventory_number_query_failed));
                }

            }
        });
    }
}